package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.ChartResult;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = {"部门接口"})
@RestController //@Controller(交给spring管理) + @ResponseBody(方法返上课了东方今典索拉卡回json格式)
@RequestMapping("/dept")
public class DepartmentController {

    /**
     * 注入service层
     */
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有的方法
     */
    @GetMapping("/treeList")
    private AjaxResult treeList() {
        //1.查询所有
        List<Department> list = departmentService.treeList();
        //2.封装对象返回
        return AjaxResult.createSuccess(list);
    }


    /**
     * 查询所有的方法
     */
    @GetMapping("/chartList")
    private AjaxResult chartList() {

        ChartResult chartData = departmentService.findChartData();

        //2.封装对象返回
        return AjaxResult.createSuccess(chartData);
    }

    /**
     * 查询所有的方法
     */
    @GetMapping
    private AjaxResult findAll() {
        long l = System.currentTimeMillis();
        //1.查询所有
        List<Department> list = departmentService.findAll();
        System.out.println("时间"+(System.currentTimeMillis()-l));
        //2.封装对象返回
        return AjaxResult.createSuccess(list);

    }

    /**
     * 查询所有的方法
     */
    @PostMapping("/findPage")
    private PageList<Department> findPage(@RequestBody DepartmentQuery departmentQuery) {

        //调用业务层 alt+enter
        //ctrl + t:快速往后找代码
        return departmentService.findPage(departmentQuery);
    }

    /**
     * 查询所有的上级部门们
     */
    @PostMapping("/findParentDepts")
    private AjaxResult findParentDepts() {

        //调用业务层 alt+enter
        return AjaxResult.createSuccess(departmentService.findParentDepts());
    }

    /**
     * 查询所有的方法
     */
    @GetMapping("/findOne/{id}")
    private AjaxResult findOne(@PathVariable("id") Long id) {

        return AjaxResult.createSuccess(departmentService.findOne(id));
    }

    /**
     * 新增或者修改  没有id就是新增  没有id就是修改
     *
     * @param department 部门对象
     *                   eclipse风格:alt+shift+z 快速生成try
     *                   ctrl+shift+enter: 快速生成;
     */
    @PostMapping
    @ApiOperation(value = "新增/修改部门", notes = "王天霸")
    public AjaxResult saveOrUpdate(@Valid @ApiParam(value = "新增/修改部门参数", required = true) @RequestBody Department department) {
        //调用service层

        departmentService.saveOrUpdate(department);
        return AjaxResult.createSuccess();

    }

    /**
     * 删除方法
     *
     * @param id 主键id
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        //调用删除方法

        departmentService.delete(id);
        return AjaxResult.createSuccess();

    }

    /**
     * 删除方法
     *
     * @param id 主键id
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody DepartmentQuery departmentQuery) {
        //调用删除方法

        departmentService.batchDelete(departmentQuery);
        return AjaxResult.createSuccess();

    }

}
